package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.dto.ArticleHomeDto;
import com.heima.article.dto.ArticleInfoDto;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApArticle2;
import com.heima.article.entity.ApArticleContent;
import com.heima.article.entity.ArticleDto;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.IApArticleContentService;
import com.heima.article.service.IApArticleService;
import com.heima.article.service.ITemplateService;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-03-14
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements IApArticleService {
    @Autowired
    private IApArticleContentService apArticleContentService;
    @Autowired
    private ITemplateService templateService;

    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
        //id都是雪花算法
        //先判断文章的id是否为空
        //为空的话，就新增文章
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto, apArticle);
        if (dto.getId() == null) {

            save(apArticle);
            //保存之后apArticle会保存数据库生成的id
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentService.save(apArticleContent);
            try {
                templateService.createHtml(apArticle,dto.getContent());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return ResponseResult.okResult(apArticle.getId());
        }
        //id不为空就到这一步
        updateById(apArticle);
        //修改内容表
        LambdaUpdateWrapper<ApArticleContent> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ApArticleContent::getArticleId, apArticle.getId())
                .set(ApArticleContent::getContent, dto.getContent());
        apArticleContentService.update(updateWrapper);
        try {
            templateService.createHtml(apArticle,dto.getContent());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.okResult(apArticle.getId());
    }

    @Override
    /**
     * id丢失的源头就在这里，实体类id策略雪花算法生成，前端用number接受Long类型，但是雪花算法是19位，所以丢失精度让前端用string接受
     * 如果修改类型，
     */
    public ResponseResult load(ArticleHomeDto dto, int type) {
        LambdaQueryWrapper<ApArticle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getChannelId() != 0, ApArticle::getChannelId, dto.getChannelId());
        if (type == 0) {
            queryWrapper.gt(ApArticle::getPublishTime, dto.getMaxTime());
        }
        if (type == 1) {
            queryWrapper.lt(ApArticle::getPublishTime, dto.getMinTime());
        }

        queryWrapper.eq(ApArticle::getIsDelete, false);
        queryWrapper.eq(ApArticle::getIsDown, false);
        queryWrapper.orderByDesc(ApArticle::getPublishTime);
        IPage<ApArticle> page = new Page<>(1, dto.getSize());
        page = page(page, queryWrapper);

        List<ApArticle> records = page.getRecords();
        ArrayList<ApArticle2> apArticle2s = new ArrayList<>(records.size());
        for (ApArticle record : records) {
            ApArticle2 apArticle2 = new ApArticle2();
            BeanUtils.copyProperties(record, apArticle2);
            apArticle2.setId(record.getId().toString());
            apArticle2s.add(apArticle2);

        }

        BeanUtils.copyProperties(records, apArticle2s);
        return ResponseResult.okResult(apArticle2s);

    }

    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {
        // 根据id查询文章表和文章内容表
        ApArticle apArticle = this.getById(dto.getArticleId());
        LambdaQueryWrapper<ApArticleContent> query = new LambdaQueryWrapper<>();
        query.eq(ApArticleContent::getArticleId, dto.getArticleId());
        ApArticleContent articleContent = apArticleContentService.getOne(query);
        // 构建响应对象
        Map<String, Object> map = new HashMap<>();
        map.put("config", apArticle);
        map.put("content", articleContent);
        return ResponseResult.okResult(map);
    }
}
